﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace MaxLeaf.DAL.W2K
{
   


    public class goods_brandDAL : BaseDAL
    {
        public goods_brandDAL()
            : base()
        {
            strK3TableName = "t_ComBrand";
            strWebTableName = "goods_brand";

            strK3MapKeyFieldName = "FitemID";
            strWebMapKeyFieldName = "brand_id";
            strWebMapKeyTableName = "k3_goods_brand";

            //strK3DestRealKeyFieldName = "FName";
            //strWebOrgDestRealKeyFieldName = "brand_name";


            strFieldNames = "FNumber,FName";

            strFItemClassID = Const.FItemClassID_Brand;
        }




        protected override void DAL_GetFieldData(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strK3FieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strK3FieldName.Equals("FitemID", StringComparison.InvariantCultureIgnoreCase))
            {
                if (CvtFunc.ToInt(drDestData[strK3FieldName]) != 0) return;
                if (CvtFunc.ToString(drDestData["FNumber"]).Length == 0)
                {
                    drDestData["FNumber"] = GetFNumber(dbOrgWebReadCon, dbOrgWebWriteCon,dbDestK3Con, drOrgData);
                }
                drDestData[strK3FieldName] = GetFItemID(dbDestK3Con, "0", CvtFunc.ToString(drDestData["FNumber"]), CvtFunc.ToString(drOrgData["brand_name"]));
            }
            else if (strK3FieldName.Equals("FNumber", StringComparison.InvariantCultureIgnoreCase))
            {
                if (CvtFunc.ToString(drDestData[strK3FieldName]).Length == 0)
                {
                    drDestData[strK3FieldName] = GetFNumber(dbOrgWebReadCon, dbOrgWebWriteCon,dbDestK3Con, drOrgData);
                }
            }
            else if (strK3FieldName.Equals("FName", StringComparison.InvariantCultureIgnoreCase))
            {
                drDestData[strK3FieldName] = drOrgData["brand_name"];
            }
            else
            {
                drDestData[strK3FieldName] = 0;
            }
        }

        private string GetFItemID( DBAccess dbDestK3Con, string strFParentID, string strFnumber, string strFName)
        {
            
            const int intFDetail = 1;
            const int intFLevel = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();



            strFItemId = dal.GetFItemId(dbDestK3Con, strFItemClassID, intFLevel, strFParentID, strFnumber, strFName, intFDetail);

            UpdateAccess(dbDestK3Con, strFItemId, strFParentID);

            return strFItemId;
        }

        private void UpdateAccess(DBAccess dbDestK3Con, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from access_t_ComBrand where FitemID=@FitemID)
                begin
                 Insert into access_t_ComBrand(FitemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FitemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
                end
                ";
            dbDestK3Con.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FitemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

        private string GetFNumber(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, DataRow drOrgData)
        {
            string strSql = "select max(FNumber) from t_ComBrand where len(FNumber)=4 and isnumeric(FNumber)=1";
            int iMax = CvtFunc.ToInt(dbDestK3Con.GetValue(strSql))+1;
            while (dbDestK3Con.GetDataFirstRow("select FNumber from t_ComBrand where FNumber=@FNumber", dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FNumber", BaseFunctions.FormatWithZero(iMax.ToString(), 4) } }).Value) != null)
            {
                iMax++;
            }

            return BaseFunctions.FormatWithZero(iMax.ToString(),4);
        }


        protected override void WriteK3Log(DBAccess dbDestK3Con, int intKeyValue)
        {
             string strSql = @"insert into mLog(logtype,keyvalue,logstatus)
                values(@logtype,@keyvalue,@logstatus)";
             dbDestK3Con.ExecuteSql(strSql, dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { 
                 {"logtype",LogType.goods_brand.ToString()}
                 ,{"keyvalue",intKeyValue.ToString()}
                 ,{"logstatus","1"}
             }).Value);
            
        }
        
    }
}